package com.base.cn.platform.os.service.order.price;

import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.order.ShopType;
import com.base.cn.platform.os.entity.order.shop.ShopData;
import com.base.cn.platform.os.service.manage.config.SysConfigBiz;
import com.base.cn.platform.os.service.order.check.CreateOrderDataFrom;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * @describe 订单价格处理器
 * @auther s.li
 * @date 2019/6/5 13:57
 */
public class OrderPriceHandle {
    public static SysConfigBiz sysConfigBiz;
    /**
     * 获取订单价格对象
     * @param createOrderDataFrom 订单数据
     * @return OrderPrice
     */
    public static OrderPrice getOrderPrice(CreateOrderDataFrom createOrderDataFrom){
        OrderPrice orderPrice = new OrderPrice();
        //商品列表
        List<ShopData> shopDataList = createOrderDataFrom.getShopList();
        double sumPrice = 0;
        double activityPrice = 0;
        double yhPrice = 0;
        double realPrice = 0;
        double memberPrice = 0;

        for(ShopData shopData : shopDataList){
            sumPrice += shopData.getOldPrice() * shopData.getBuyNum();
            activityPrice += shopData.getActivitySubPrice();
            yhPrice += shopData.getDiscountSubPrice();
            memberPrice += shopData.getMemberPrice() ;
            //如果订单免费不计算
            if(!createOrderDataFrom.isFree()){
                realPrice += shopData.getRealPrice();
            }
        }
        orderPrice.setSumPrice(sumPrice);
        orderPrice.setActivityPrice(activityPrice);
        orderPrice.setYhPrice(yhPrice);
        orderPrice.setDiscountPrice(yhPrice+activityPrice+memberPrice);
        //计算会员优惠
        if(!createOrderDataFrom.isFree()&&createOrderDataFrom.isMemberUser()){
            orderPrice.setMemberSubPrice(sumPrice-yhPrice-activityPrice-memberPrice);
            orderPrice.setRealPrice(memberPrice);
        }else{
            orderPrice.setMemberSubPrice(0);
            orderPrice.setRealPrice(realPrice);
        }


        Map<String,List<ShopData>> shopDataListMap = shopDataList.stream().collect(Collectors.groupingBy(e->e.getShopType()));
        //获取图书商品
        List<ShopData> bookShopDataList = shopDataListMap.get(ShopType.ORDER_SHOP_BOOK);
        if(ObjectUtils.isNotEmpty(bookShopDataList)){//如果有图书商品，则获取运费
            TreeMap<String,String> freightMap = sysConfigBiz.getSysConfigContext(ConfigName.FREIGHT);
            double freightPrice = Double.parseDouble(freightMap.get("price"));
            orderPrice.setFreightPrice(freightPrice);
        }
        return orderPrice;
    }
}
